package edu.example.Absence.servlet;

import edu.example.Absence.Dao.absenceDao;
import edu.example.Absence.bean.absence;
import edu.example.bean.buildings;
import edu.example.house.dao.housemanageDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/updateAbsence")
public class AbsenceUpdate extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<buildings> buildings = new absenceDao().getAllBuildings();
        req.setAttribute("buildings", buildings);
        Integer user_id = Integer.parseInt(req.getParameter("id"));
        List<absence> list = new absenceDao().findById(user_id);
        req.setAttribute("list",list);
        req.getRequestDispatcher("/absences/absenceUpdate.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id= Integer.parseInt(req.getParameter("user_id"));
        String building = req.getParameter("building");
        String room = req.getParameter("room");
        int affectrows = new absenceDao().absenceUpdate(id,building,room);
        if(affectrows>0){
            /*resp.getWriter().println("<script> location.href='/JavaWebStudentManager/absence'</script>");*/
            resp.sendRedirect("absence");
        }
    }
}
